package sk.vasicek.drinkagent.service;

import java.util.ArrayList;
import java.util.Date;

import sk.vasicek.drinkagent.domain.DrinkEntry;
import sk.vasicek.drinkagent.domain.Drinker;
import android.util.Log;

public class DrinkStatService {

	private static final String TAG = DrinkStatService.class.getSimpleName();

	// public float calculateAlcoholConcentraton(ArrayList<Drink> drinks,
	// Drinker drinker) {
	// float alcoholConcentration;
	// float avgDrinkConcentration = 0;
	// final float ethanolConcentration = new Float("0.8");
	//
	// for (Drink drink : drinks) {
	// avgDrinkConcentration += (drink.getVolume() *
	// drink.getAlcoholConcentration() * ethanolConcentration);
	// }
	// alcoholConcentration = ((avgDrinkConcentration / (drinker.getWeight() *
	// drinker.getSex().getAlcoholMassConstant())));
	// Log.v(this.getClass().getSimpleName(), "concentration=" +
	// alcoholConcentration);
	// return alcoholConcentration;
	// }
	public static int dataSize = 150;

	public static final double MILLIS_TO_HOURS_DIVIDER = 3600000l;
	public static final double ETHANOL_CONCENTRATION = 0.8d;
	public static final double ETHANOL_HOURLY_DECREASE = 0.0015d;

	public double calculateAlcoholConcentration(ArrayList<DrinkEntry> drinks, Drinker drinker) {
		return calculateAlcoholConcentration(drinks, drinker, 24l);
	}

	public double calculateAlcoholConcentration(ArrayList<DrinkEntry> drinks, Drinker drinker, long startHoursBack) {
		double alcoholConcentration;
		double avgDrinkConcentration = 0;
		long startTime = new Date().getTime() - (long) MILLIS_TO_HOURS_DIVIDER * startHoursBack;

		for (DrinkEntry drink : drinks) {
			if (drink.getTimeDrinked().after(new Date((startTime)))) {
				double ethanolDecreased = (float) (ETHANOL_HOURLY_DECREASE * ((double) (new Date().getTime() - drink.getTimeDrinked().getTime()) / MILLIS_TO_HOURS_DIVIDER));
				// float drinkAlcoholInTime = (float) ((drink.getVolume() *
				// drink.getConcentration() * ETHANOL_CONCENTRATION) -
				// ethanolDecreased);
				// // if (drinkAlcoholInTime > 0)
				// avgDrinkConcentration += drinkAlcoholInTime;
				if (ethanolDecreased > 0) {
					double drinkAlcoholInTime = ((drink.getVolume() * drink.getConcentration() * ETHANOL_CONCENTRATION) - ethanolDecreased);
					if (drinkAlcoholInTime > 0f)
						avgDrinkConcentration += drinkAlcoholInTime;
				}
				// else {
				// double drinkAlcoholInTime = ((drink.getVolume() *
				// drink.getConcentration() * ETHANOL_CONCENTRATION) +
				// ethanolDecreased);
				// if (drinkAlcoholInTime > 0)
				// avgDrinkConcentration += drinkAlcoholInTime;
				// }
			}
		}
		alcoholConcentration = ((avgDrinkConcentration / (drinker.getWeight() * drinker.getSex().getAlcoholMassConstant())));
		Log.v(this.getClass().getSimpleName(), "concentration=" + alcoholConcentration);
		if (alcoholConcentration > 0f)
			return alcoholConcentration;
		else
			return 0d;

	}

	public float calculateFinalPrice(ArrayList<DrinkEntry> drinks) {
		return calculateFinalPrice(drinks, 24l);
	}

	public float calculateFinalPrice(ArrayList<DrinkEntry> drinks, long startHoursBack) {
		float finalPrice = 0;
		long startTime = (long) (new Date().getTime() - MILLIS_TO_HOURS_DIVIDER * startHoursBack);

		for (DrinkEntry drink : drinks) {
			if (drink.getTimeDrinked().after(new Date((startTime)))) {
				finalPrice += drink.getPrice();
			}
		}
		Log.v(TAG, "finalPrice=" + finalPrice);
		return finalPrice;
	}

	public Float[] generateAlcoholConcentrationGraphInput(ArrayList<DrinkEntry> drinks, Drinker drinker, ArrayList<String> timeLabels) {
		return generateAlcoholConcentrationGraphInput(drinks, drinker, 24l, 40l, timeLabels);
	}

	public Float[] generateAlcoholConcentrationGraphInput(ArrayList<DrinkEntry> drinks, Drinker drinker, long startHoursBack, long timeWindowHours, ArrayList<String> timeLabels) {
		double alcoholConcentration;
		double avgDrinkConcentration = 0;
		// timeLabels = new ArrayList<String>();
		Float concentrationValues[] = new Float[dataSize];
		long startTime = (long) (new Date().getTime() - MILLIS_TO_HOURS_DIVIDER * startHoursBack);
		double step = (MILLIS_TO_HOURS_DIVIDER * timeWindowHours) / (double) dataSize;
		for (int i = 0; i < dataSize; i++) {
			avgDrinkConcentration = 0;
			for (DrinkEntry drink : drinks) {
				// if (drink.getTimeDrinked().after(new Date((startTime + step *
				// i)))) {
				if (drink.getTimeDrinked().after(new Date((startTime)))) {
					double ethanolDecreased = (ETHANOL_HOURLY_DECREASE * (((startTime + step * i) - drink.getTimeDrinked().getTime()) / MILLIS_TO_HOURS_DIVIDER));
					if (ethanolDecreased > 0) {
						double drinkAlcoholInTime = ((drink.getVolume() * drink.getConcentration() * ETHANOL_CONCENTRATION) - ethanolDecreased);
						if (drinkAlcoholInTime > 0d)
							avgDrinkConcentration += drinkAlcoholInTime;
					}
					// else {
					// double drinkAlcoholInTime = ((drink.getVolume() *
					// drink.getConcentration() * ETHANOL_CONCENTRATION) +
					// ethanolDecreased);
					// if (drinkAlcoholInTime > 0d)
					// avgDrinkConcentration += drinkAlcoholInTime;
					// }
				}
			}
			alcoholConcentration = ((avgDrinkConcentration / (drinker.getWeight() * drinker.getSex().getAlcoholMassConstant())));
			if (i % 20 == 0) {
				Date time = new Date((long) (new Date().getTime() - ((startTime + step * i))));
				timeLabels.add(time.getHours() + ":" + time.getMinutes());
			}
			Log.v(this.getClass().getSimpleName(), "concentration=" + alcoholConcentration);
			if (alcoholConcentration > 0f)
				concentrationValues[i] = (float) alcoholConcentration;
			else
				concentrationValues[i] = 0f;
		}
		return concentrationValues;
	}
}
